﻿using System;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Factory;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model.Thrift
{
    /// <summary>
    /// A query for the thrift call get_range_slices for subcolumns of supercolumns
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="SN"></typeparam>
    /// <typeparam name="N"></typeparam>
    /// <typeparam name="V"></typeparam>
    public class ThriftRangeSubSlicesQuery<K, SN, N, V> : AbstractSliceQuery<K, N, V, OrderedRows<K, N, V>>, RangeSubSlicesQuery<K, SN, N, V>
    {
        public ThriftRangeSubSlicesQuery(Keyspace k, Serializer<K> keySerializer,
            Serializer<SN> sNameSerializer, Serializer<N> nameSerializer, Serializer<V> valueSerializer)
            : base(k, keySerializer, nameSerializer, valueSerializer)
        {
            if (sNameSerializer == null)
            {
                throw new NullReferenceException("SuperNameSerializer name cannot be null");
            }
            SuperNameSerializer = sNameSerializer;
            KeyRange = new HKeyRange<K>(keySerializer);
        }
        public Serializer<SN> SuperNameSerializer
        {
            get;
            set;
        }
        protected HKeyRange<K> KeyRange
        {
            get;
            set;
        }
        public SN SuperColumn
        {
            get;
            set;
        }
        public RangeSubSlicesQuery<K,SN,N,V> SetKeys(K start, K end)
        {
            KeyRange.SetKeys(start, end);
            return this;
        }
        public RangeSubSlicesQuery<K,SN,N,V> SetRowCount(int rowCount)
        {
            KeyRange.RowCount = rowCount;
            return this;
        }
        public RangeSubSlicesQuery<K, SN, N, V> SetSuperColumn(SN sc)
        {
            if (sc == null)
            {
                throw new NullReferenceException("SuperColumn cannot be null");
            }
            SuperColumn = sc;
            return this;
        }
        public override string ToString()
        {
            return "RangeSuperSlicesQuery(" + KeyRange + base.ToStringInternal() + ")";
        }
        public new RangeSubSlicesQuery<K, SN, N, V> SetColumnNames(List<N> columnNames)
        {
            return (RangeSubSlicesQuery<K, SN, N, V>) base.SetColumnNames(columnNames);
        }
        public new RangeSubSlicesQuery<K, SN, N, V> SetRange(N start, N finish, bool reversed, int count)
        {
            return (RangeSubSlicesQuery<K, SN, N, V>) base.SetRange(start, finish, reversed, count);
        }
        public new RangeSubSlicesQuery<K, SN, N, V> SetColumnFamily(string cf)
        {
            return (RangeSubSlicesQuery<K, SN, N, V>) base.SetColumnFamily(cf);
        }
        public override QueryResult<OrderedRows<K,N,V>> Execute()
        {
            if (ColumnFamilyName == null || SuperColumn == null)
            {
                throw new NullReferenceException("ColumnFamilyName  and SuperColumn cannot be null");
            }
            Func<KeyspaceService, OrderedRows<K,N,V>> x = (KeyspaceService ks) =>
            {
                ColumnParent columnParent = new ColumnParent(ColumnFamilyName);
                columnParent.Super_column = SuperNameSerializer.ToBytes(SuperColumn);
                Dictionary<K, List<Column>> thriftRet = KeySerializer.FromBytesDictionary(
                    ks.GetRangeSlices(columnParent, GetPredicate(), KeyRange.ToThrift()));
                return new OrderedRowsImpl<K,N,V>(thriftRet, ColumnNameSerializer, ValueSerializer);
            };
            return new QueryResultImpl<OrderedRows<K,N,V>>(
                Keyspace.DoExecute(new KeyspaceOperationCallbackBuilder<OrderedRows<K,N,V>>(x)), this);
        }
    }
}
